﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DbUpgrade.General
{
    public class CodeHelper
    {
        #region 属性

        #region 当前目录
        private static string _currentPath = Path.GetDirectoryName(typeof(CodeHelper).Assembly.Location);
        /// <summary>
        /// 当前目录
        /// </summary>
        public static string CurrentPath
        {
            get { return _currentPath; }
        }
        #endregion

        #region 当前文件目录
        private static string _currentDocPath = System.Windows.Forms.Application.StartupPath;
        /// <summary>
        /// 当前文件目录
        /// </summary>
        public static string CurrentDocPath
        {
            get { return _currentDocPath; }
        }
        #endregion

        #endregion

        #region 外部方法

        #region 文件注释获取占位字符串
        /// <summary>
        /// 获取占位字符串
        /// </summary>
        /// <param name="aSValue"></param>
        /// <param name="ac"></param>
        /// <returns></returns>
        public static string GetFileStrNone(string aSValue, Char ac)
        {
            string result = string.Empty;
            int vilen = 90;
            int viValuelen = trueLength(aSValue);
            if (viValuelen > vilen)
            {
                string str = string.Empty;
                string str2 = string.Empty;
                for (int i = 0; i < aSValue.Length; i++)
                {
                    str = aSValue.Substring(0, i);
                    if (trueLength(str) > vilen - 2)
                    {
                        str2 = aSValue.Substring(i, aSValue.Length - i);
                        break;
                    }
                }
                result += GetFileStrNone(str, ac);
                result += GetFileStrNone("          " + str2, ac);
            }
            else
            {
                int viTlen = vilen - viValuelen;
                string vsReturn = "";
                for (int i = 0; i < viTlen; i++)
                {
                    vsReturn = vsReturn + ac;
                }
                result = "/* " + aSValue + vsReturn + " */\r\n";
            }
            return result;
        }
        #endregion

        #endregion

        #region 内部方法

        #region 获取字符串的长度
        /// <summary>
        /// 获取字符串的长度
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static private int trueLength(string str)
        {

            // str 字符串
            // return 字符串的字节长度
            int lenTotal = 0;
            int n = str.Length;
            string strWord = "";
            int asc;
            for (int i = 0; i < n; i++)
            {
                strWord = str.Substring(i, 1);
                asc = Convert.ToChar(strWord);
                if (asc < 0 || asc > 127)
                    lenTotal = lenTotal + 2;
                else
                    lenTotal = lenTotal + 1;
            }

            return lenTotal;
        }
        #endregion

        #endregion
    }
}
